package Graphics;

import ClientGraphics.GraphicsMain;
import GameEngine.GameConstants;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import GameEngine.GameConstants.Levels;
import GameEngine.GameConstants.Special;
import GameEngine.InputMap;
import GameEngine.Level;
import LevelEditor.XmlIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import org.dom4j.DocumentException;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author crdunwel
 */
public class MainGui extends javax.swing.JFrame
{
    // INSTANCE VARIABLES
    public CardLayout cl;                                   // layout for graphical components
    public String level;                                    // filepath for level
    public File customLevel;                                // file for custom level
    public String port;                                     // string representation for port number
    public String server;                                   // string representation for server address
    public String username;                                 // string for username
    public String type;                                     // string for user type
    public GameEngine.Client client;                        // reference to client object for player
    public ServerGUI servergui;                             // reference to the serverGUI for player
    public int portnum;                                     // portnumber selected by client
    private Image orig;
    private InputMap inputMap;                              // map for how user enters input in game

    // IMAGE VARIABLES
    public ImageIcon creIcon1,creIcon2,creIcon3;
    public ImageIcon joiIcon1,joiIcon2,joiIcon3;
    public ImageIcon sucIcon1,sucIcon2,sucIcon3;
    public ImageIcon levIcon1,levIcon2,levIcon3;
    public ImageIcon quitIcon1,quitIcon2,quitIcon3;
    public ImageIcon browIcon1, browIcon2, browIcon3;
    public ImageIcon crGameIcon1, crGameIcon2, crGameIcon3;
    public ImageIcon crGameBackIcon1, crGameBackIcon2;
    public ImageIcon connIcon1,connIcon2, connIcon3;
    public ImageIcon failedConnect, badServer, badPortNum, stringPort;
    public ImageIcon badPortNum_Create, badServer_Create, stringPort_Create, customGameImg;
    public ImageIcon readyIcon1, readyIcon2, readyIcon3;
    private BufferedImage mainImg;
    private BufferedImage creaImg;
    private BufferedImage joinImg;
    private BufferedImage connImg;
    private BufferedImage servImg;
    private BufferedImage pixiepoo_transmute;
    public ImageIcon invalidXMLIcon;

    // STATIC FINAL VARIABLES
    private final static int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB;
    private final int IMG_WIDTH =1024;
    private final int IMG_HEIGHT = 791;
    public static final String buttonpath = "Images/Buttons/";
    public static final String backgroundpath = "Images/Menus/";
    public static final String menuimagepath = "Images/MenuImages/";
    public static final String spritepath = "Images/Sprites/";
    public static final String levelpath = "Levels/";

    // GRAPHICS DRAWER
    public GraphicsMain gMain;

    /**
     * CONSTRUCTOR
     *
     * The MainGui is the object responsible for graphical interfacing with the player
     */
    public MainGui()
    {
        gMain = new GraphicsMain(Levels.ABSTRACT,new Dimension(IMG_WIDTH, IMG_HEIGHT), 40, 40);
        this.setPreferredSize(new Dimension(IMG_WIDTH, IMG_HEIGHT));
        initImages();
        initComponents();
        XmlFileFilter xmlfilter = new XmlFileFilter();
        levelChooser.addChoosableFileFilter(xmlfilter);
    }
    
    // GETTERS AND SETTERS
    //****************************//

    public JPanel getGamePanel()
    {
        return game;
    }

    public JPanel getContainPanel()
    {
        return containPanel;
    }

   //****************************//

    /**
     * This method initializes all the images that will be used in the GUI menu by using
     * ImageIO to read them into a BufferedImage
     */
    public void initImages()
    {
        Image orig1, orig2, orig3, orig4, orig5;
        orig1 = null;
        orig2 = null;
        orig3 = null;
        orig4 = null;
        orig5 = null;
        try
        {
            orig1 = ImageIO.read(new File(backgroundpath + "pixiepoo_intro.jpg"));
            orig2 = ImageIO.read(new File(backgroundpath + "creategame.jpg"));
            orig3 = ImageIO.read(new File(backgroundpath + "joingame.jpg"));
            orig4 = ImageIO.read(new File(backgroundpath + "connectgame.jpg"));
            orig5 = ImageIO.read(new File(backgroundpath + "basic.jpg"));
        }
        catch(IOException e)
        {
            System.out.println("Failed to load background image"+e.getMessage());
        }
        // BACKGROUND IMAGES
        mainImg = resizeImage(orig1,DEFAULT_IMAGE_TYPE);
        creaImg = resizeImage(orig2,DEFAULT_IMAGE_TYPE);
        joinImg = resizeImage(orig3,DEFAULT_IMAGE_TYPE);
        connImg = resizeImage(orig4,DEFAULT_IMAGE_TYPE);
        servImg = resizeImage(orig5, DEFAULT_IMAGE_TYPE);
        // MAIN MENU BUTTONS
        creIcon1 = new ImageIcon(buttonpath + "creategame_button_normal.gif");
        creIcon2 = new ImageIcon(buttonpath + "creategame_button_mouseover.jpg");
        creIcon3 = new ImageIcon(buttonpath + "creategame_button_pressed.jpg");
        joiIcon1 = new ImageIcon(buttonpath + "joinbutton_normal.gif");
        joiIcon2 = new ImageIcon(buttonpath + "joinbutton_mouseover.jpg");
        joiIcon3 = new ImageIcon(buttonpath + "joinbutton_pressed.jpg");
        levIcon1 = new ImageIcon(buttonpath + "leveleditorbutton_normal.gif");
        levIcon2 = new ImageIcon(buttonpath + "leveleditorbutton_mouseover.jpg");
        levIcon3 = new ImageIcon(buttonpath + "leveleditorbutton_pressed.jpg");
        quitIcon1 = new ImageIcon(buttonpath + "quitbutton_normal.gif");
        quitIcon2 = new ImageIcon(buttonpath + "quitbutton_mouseover.jpg");
        quitIcon3 = new ImageIcon(buttonpath + "quitbutton_pressed.jpg");
        // CREATE GAME BUTTONS
        browIcon1 = new ImageIcon(buttonpath + "browse_button_normal.gif");
        browIcon2 = new ImageIcon(buttonpath + "browse_button_mouseover.gif");
        browIcon3 = new ImageIcon(buttonpath + "browse_button_pressed.gif");
        crGameIcon1 = new ImageIcon(buttonpath + "creategame2_button_normal.jpg");
        crGameIcon2 = new ImageIcon(buttonpath + "creategame2_button_mouseover.jpg");
        crGameIcon3 = new ImageIcon(buttonpath + "creategame2_button_pressed.jpg");
        crGameBackIcon1 = new ImageIcon(buttonpath + "back_button_normal.gif");
        crGameBackIcon2 = new ImageIcon(buttonpath + "back_button_mouseover.gif");
        // JOIN GAME BUTTONS
        connIcon1 = new ImageIcon(buttonpath + "trytoconnect_button_normal.jpg");
        connIcon2 = new ImageIcon(buttonpath + "trytoconnect_button_mouseover.jpg");
        connIcon3 = new ImageIcon(buttonpath + "trytoconnect_button_pressed.jpg");
        // JOIN GAME IMAGES
        failedConnect = new ImageIcon(menuimagepath + "failedtoConnect.gif");
        badPortNum = new ImageIcon(menuimagepath + "badPortNumber.gif");
        stringPort = new ImageIcon(menuimagepath + "noNumberPortEntered.gif");
        badServer = new ImageIcon(menuimagepath + "noServerEntered.gif");
        readyIcon1 = new ImageIcon(buttonpath + "readytoplay_normal.jpg");
        readyIcon2 = new ImageIcon(buttonpath + "readytoplay_mouseover.jpg");
        //CREATE GAME IMAGES
        badPortNum_Create = new ImageIcon(menuimagepath + "badPortNumber_create.gif");
        badServer_Create = new ImageIcon(menuimagepath + "badServer_create.gif");
        stringPort_Create = new ImageIcon(menuimagepath + "noPortEntered_create.gif");
        customGameImg = new ImageIcon(menuimagepath + "customLevel.gif");
        invalidXMLIcon = new ImageIcon(menuimagepath + "invalidXML.gif");
    }

    /**
     * This method resizes an image.
     *
     * @param originalImage
     * @param type
     * @return
     */
     public BufferedImage resizeImage(Image originalImage, int type)
     {
	    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
	    Graphics2D g = resizedImage.createGraphics();
	    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
	    g.dispose();
	    return resizedImage;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        levelChooser = new javax.swing.JFileChooser(levelpath);
        msgBox = new javax.swing.JOptionPane();
        containPanel = new javax.swing.JPanel();
        mainPane = new javax.swing.JPanel()
        {    @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(mainImg,0,0,null);
            }
        }
        ;
        creGameButton = new javax.swing.JButton();
        levEditorButton = new javax.swing.JButton();
        joiButton = new javax.swing.JButton();
        quitGameButton = new javax.swing.JButton();
        createGamePane = new javax.swing.JPanel()
        { @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(creaImg, 0, 0, null);
            }
        }
        ;
        levelSelectionComboBox = new javax.swing.JComboBox();
        cusLevelField = new javax.swing.JTextField();
        seleCustomLevel = new javax.swing.JButton();
        portField = new javax.swing.JTextField();
        serAddrField = new javax.swing.JTextField();
        createButton = new javax.swing.JButton();
        createBackButton = new javax.swing.JButton();
        portErrorImageCreate = new javax.swing.JLabel();
        serverErrorImageCreate = new javax.swing.JLabel();
        customGame = new javax.swing.JLabel();
        XMLErrorLabel = new javax.swing.JLabel();
        joinGamePane = new javax.swing.JPanel()
        { @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(joinImg, 0, 0, null);
            }
        }
        ;
        connectButton = new javax.swing.JButton();
        joinPortField = new javax.swing.JTextField();
        joinServerField = new javax.swing.JTextField();
        joinBackButton = new javax.swing.JButton();
        failedToConnectImage = new javax.swing.JLabel();
        portErrorImage = new javax.swing.JLabel();
        serverErrorImage = new javax.swing.JLabel();
        successPane = new javax.swing.JPanel()
        {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(connImg, 0, 0, null);
            }
        }
        ;
        userNameField = new javax.swing.JTextField();
        playerTypeCb = new javax.swing.JComboBox();
        readyButton = new javax.swing.JButton();
        AnimationPane = new javax.swing.JPanel();
        game = gMain.getGraphicsPanel() ;
        victoryPane = new javax.swing.JPanel();
        replayLevelButton = new javax.swing.JButton();
        backToMainMenuButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1024, 790));
        setResizable(false);

        containPanel.setMinimumSize(new java.awt.Dimension(1024, 790));
        containPanel.setPreferredSize(new java.awt.Dimension(1024, 790));
        containPanel.setLayout(new java.awt.CardLayout());

        mainPane.setMaximumSize(new java.awt.Dimension(1024, 790));
        mainPane.setMinimumSize(new java.awt.Dimension(1024, 790));
        mainPane.setPreferredSize(new java.awt.Dimension(1024, 790));

        creGameButton.setIcon(creIcon1);
        creGameButton.setRolloverIcon(creIcon2);
        creGameButton.setPressedIcon(creIcon3);
        creGameButton.setOpaque(false);
        creGameButton.setFocusPainted(false);
        creGameButton.setBorderPainted(false);
        creGameButton.setContentAreaFilled(false);
        creGameButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        creGameButton.setBorder(null);
        creGameButton.setBorderPainted(false);
        creGameButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        creGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                creGameButtonActionPerformed(evt);
            }
        });

        levEditorButton.setIcon(levIcon1);
        levEditorButton.setRolloverIcon(levIcon2);
        levEditorButton.setPressedIcon(levIcon3);
        levEditorButton.setOpaque(false);
        levEditorButton.setFocusPainted(false);
        levEditorButton.setBorderPainted(false);
        levEditorButton.setContentAreaFilled(false);
        levEditorButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        levEditorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                levEditorButtonActionPerformed(evt);
            }
        });

        joiButton.setIcon(joiIcon1);
        joiButton.setRolloverIcon(joiIcon2);
        joiButton.setPressedIcon(joiIcon3);
        joiButton.setOpaque(false);
        joiButton.setFocusPainted(false);
        joiButton.setBorderPainted(false);
        joiButton.setContentAreaFilled(false);
        joiButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        joiButton.setMaximumSize(new java.awt.Dimension(129, 29));
        joiButton.setMinimumSize(new java.awt.Dimension(129, 29));
        joiButton.setPreferredSize(new java.awt.Dimension(129, 29));
        joiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joiButtonActionPerformed(evt);
            }
        });

        quitGameButton.setIcon(quitIcon1);
        quitGameButton.setRolloverIcon(quitIcon2);
        quitGameButton.setPressedIcon(quitIcon3);
        quitGameButton.setOpaque(false);
        quitGameButton.setFocusPainted(false);
        quitGameButton.setBorderPainted(false);
        quitGameButton.setContentAreaFilled(false);
        quitGameButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        quitGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitGameButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPaneLayout = new javax.swing.GroupLayout(mainPane);
        mainPane.setLayout(mainPaneLayout);
        mainPaneLayout.setHorizontalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPaneLayout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(levEditorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quitGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(creGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(joiButton, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(706, Short.MAX_VALUE))
        );

        mainPaneLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {joiButton, levEditorButton});

        mainPaneLayout.setVerticalGroup(
            mainPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPaneLayout.createSequentialGroup()
                .addContainerGap(243, Short.MAX_VALUE)
                .addComponent(joiButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(creGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(levEditorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(quitGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93))
        );

        mainPaneLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {joiButton, levEditorButton});

        containPanel.add(mainPane, "Main");

        createGamePane.setMaximumSize(new java.awt.Dimension(1024, 790));
        createGamePane.setMinimumSize(new java.awt.Dimension(1024, 790));
        createGamePane.setPreferredSize(new java.awt.Dimension(1024, 790));
        createGamePane.setRequestFocusEnabled(false);

        levelSelectionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PIXRAIN", "GEORGEWASHINGTON", "EINSTEIN", "ABSTRACT", "CRAZY_COLORS" }));
        levelSelectionComboBox.setPreferredSize(new java.awt.Dimension(90, 40));
        cusLevelField.setEditable(false);
        cusLevelField.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 18)); // NOI18N
        cusLevelField.setVisible(true);
        seleCustomLevel.setIcon(browIcon1);
        seleCustomLevel.setRolloverIcon(browIcon2);
        seleCustomLevel.setPressedIcon(browIcon3);
        seleCustomLevel.setOpaque(false);
        seleCustomLevel.setFocusPainted(false);
        seleCustomLevel.setBorderPainted(false);
        seleCustomLevel.setContentAreaFilled(false);
        seleCustomLevel.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        seleCustomLevel.setMaximumSize(new java.awt.Dimension(120, 44));
        seleCustomLevel.setMinimumSize(new java.awt.Dimension(120, 44));
        seleCustomLevel.setPreferredSize(new java.awt.Dimension(120, 44));
        seleCustomLevel.setVisible(true);
        seleCustomLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seleCustomLevelActionPerformed(evt);
            }
        });

        portField.setText("4343");

        createButton.setIcon(crGameIcon1);
        createButton.setRolloverIcon(crGameIcon2);
        createButton.setPressedIcon(crGameIcon3);
        createButton.setOpaque(false);
        createButton.setFocusPainted(false);
        createButton.setBorderPainted(false);
        createButton.setContentAreaFilled(false);
        createButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        createButton.setMaximumSize(new java.awt.Dimension(200, 80));
        createButton.setMinimumSize(new java.awt.Dimension(200, 80));
        createButton.setPreferredSize(new java.awt.Dimension(200, 80));
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        createBackButton.setIcon(crGameBackIcon1);
        createBackButton.setRolloverIcon(crGameBackIcon2);
        createBackButton.setOpaque(false);
        createBackButton.setFocusPainted(false);
        createBackButton.setBorderPainted(false);
        createBackButton.setContentAreaFilled(false);
        createBackButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        createBackButton.setMaximumSize(new java.awt.Dimension(180, 99));
        createBackButton.setMinimumSize(new java.awt.Dimension(180, 99));
        createBackButton.setPreferredSize(new java.awt.Dimension(180, 99));
        createBackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createBackButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout createGamePaneLayout = new javax.swing.GroupLayout(createGamePane);
        createGamePane.setLayout(createGamePaneLayout);
        createGamePaneLayout.setHorizontalGroup(
            createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(createGamePaneLayout.createSequentialGroup()
                .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(createGamePaneLayout.createSequentialGroup()
                        .addGap(143, 143, 143)
                        .addComponent(portErrorImageCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(createGamePaneLayout.createSequentialGroup()
                        .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(createGamePaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, createGamePaneLayout.createSequentialGroup()
                                        .addGap(113, 113, 113)
                                        .addComponent(serverErrorImageCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, createGamePaneLayout.createSequentialGroup()
                                        .addComponent(customGame, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cusLevelField, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                                            .addComponent(levelSelectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(createGamePaneLayout.createSequentialGroup()
                                                .addGap(10, 10, 10)
                                                .addComponent(XMLErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(22, 22, 22))
                            .addGroup(createGamePaneLayout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(createBackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(createButton, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(portField, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                                .addComponent(serAddrField))
                            .addComponent(seleCustomLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(203, 203, 203)))
                .addContainerGap())
        );
        createGamePaneLayout.setVerticalGroup(
            createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, createGamePaneLayout.createSequentialGroup()
                .addGap(208, 208, 208)
                .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cusLevelField, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(seleCustomLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(XMLErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(createGamePaneLayout.createSequentialGroup()
                        .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(levelSelectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(customGame, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(83, 83, 83)
                        .addComponent(portErrorImageCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(serverErrorImageCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addComponent(serAddrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(createGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(createButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(createBackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        createGamePaneLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {portField, serAddrField});

        containPanel.add(createGamePane, "Create");

        joinGamePane.setPreferredSize(new java.awt.Dimension(1024, 790));

        connectButton.setIcon(connIcon1);
        connectButton.setRolloverIcon(connIcon2);
        connectButton.setPressedIcon(connIcon3);
        connectButton.setOpaque(false);
        connectButton.setFocusPainted(false);
        connectButton.setBorderPainted(false);
        connectButton.setContentAreaFilled(false);
        connectButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        joinPortField.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 14));

        joinServerField.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 14));

        joinBackButton.setIcon(crGameBackIcon1);
        joinBackButton.setRolloverIcon(crGameBackIcon2);
        joinBackButton.setOpaque(false);
        joinBackButton.setFocusPainted(false);
        joinBackButton.setBorderPainted(false);
        joinBackButton.setContentAreaFilled(false);
        joinBackButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        joinBackButton.setMaximumSize(new java.awt.Dimension(275, 165));
        joinBackButton.setMinimumSize(new java.awt.Dimension(275, 165));
        joinBackButton.setPreferredSize(new java.awt.Dimension(275, 165));
        joinBackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinBackButtonActionPerformed(evt);
            }
        });

        failedToConnectImage.setPreferredSize(new java.awt.Dimension(600, 250));

        javax.swing.GroupLayout joinGamePaneLayout = new javax.swing.GroupLayout(joinGamePane);
        joinGamePane.setLayout(joinGamePaneLayout);
        joinGamePaneLayout.setHorizontalGroup(
            joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(joinGamePaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(joinBackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 234, Short.MAX_VALUE)
                .addComponent(failedToConnectImage, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(joinGamePaneLayout.createSequentialGroup()
                .addGap(440, 440, 440)
                .addGroup(joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addComponent(joinServerField)
                    .addComponent(joinPortField))
                .addGap(34, 34, 34)
                .addGroup(joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(serverErrorImage, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portErrorImage, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        joinGamePaneLayout.setVerticalGroup(
            joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(joinGamePaneLayout.createSequentialGroup()
                .addGap(206, 206, 206)
                .addGroup(joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(joinPortField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portErrorImage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(joinServerField, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(serverErrorImage, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50)
                .addComponent(connectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(joinGamePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, joinGamePaneLayout.createSequentialGroup()
                        .addComponent(joinBackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, joinGamePaneLayout.createSequentialGroup()
                        .addComponent(failedToConnectImage, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))))
        );

        joinGamePaneLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {joinPortField, joinServerField});

        containPanel.add(joinGamePane, "Join");

        successPane.setPreferredSize(new java.awt.Dimension(1024, 768));

        userNameField.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 14));
        userNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameFieldActionPerformed(evt);
            }
        });

        playerTypeCb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TRANSMUTER", "TIME", "EXPLODER", "BUILDER" }));

        readyButton.setIcon(readyIcon1);
        readyButton.setRolloverIcon(readyIcon2);
        //readyButton.setPressedIcon(readyIcon3);
        readyButton.setOpaque(false);
        readyButton.setFocusPainted(false);
        readyButton.setBorderPainted(false);
        readyButton.setContentAreaFilled(false);
        readyButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        readyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                readyButtonActionPerformed(evt);
            }
        });

        AnimationPane.setOpaque(false);

        javax.swing.GroupLayout AnimationPaneLayout = new javax.swing.GroupLayout(AnimationPane);
        AnimationPane.setLayout(AnimationPaneLayout);
        AnimationPaneLayout.setHorizontalGroup(
            AnimationPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 304, Short.MAX_VALUE)
        );
        AnimationPaneLayout.setVerticalGroup(
            AnimationPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 128, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout successPaneLayout = new javax.swing.GroupLayout(successPane);
        successPane.setLayout(successPaneLayout);
        successPaneLayout.setHorizontalGroup(
            successPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, successPaneLayout.createSequentialGroup()
                .addContainerGap(446, Short.MAX_VALUE)
                .addGroup(successPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(readyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(successPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(playerTypeCb, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(userNameField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
                .addGap(328, 328, 328))
            .addGroup(successPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(AnimationPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(710, Short.MAX_VALUE))
        );
        successPaneLayout.setVerticalGroup(
            successPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(successPaneLayout.createSequentialGroup()
                .addGap(262, 262, 262)
                .addComponent(userNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(playerTypeCb, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70)
                .addComponent(readyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(AnimationPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );

        containPanel.add(successPane, "clientConnectPane");

        javax.swing.GroupLayout gameLayout = new javax.swing.GroupLayout(game);
        game.setLayout(gameLayout);
        gameLayout.setHorizontalGroup(
            gameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1024, Short.MAX_VALUE)
        );
        gameLayout.setVerticalGroup(
            gameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 790, Short.MAX_VALUE)
        );

        containPanel.add(game, "Game");

        victoryPane.setMaximumSize(new java.awt.Dimension(1024, 768));
        victoryPane.setMinimumSize(new java.awt.Dimension(1024, 768));
        victoryPane.setPreferredSize(new java.awt.Dimension(1024, 768));

        replayLevelButton.setText("Replay Level");
        replayLevelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replayLevelButtonActionPerformed(evt);
            }
        });

        backToMainMenuButton.setText("Main Menu");

        javax.swing.GroupLayout victoryPaneLayout = new javax.swing.GroupLayout(victoryPane);
        victoryPane.setLayout(victoryPaneLayout);
        victoryPaneLayout.setHorizontalGroup(
            victoryPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, victoryPaneLayout.createSequentialGroup()
                .addGap(312, 312, 312)
                .addGroup(victoryPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(backToMainMenuButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addComponent(replayLevelButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE))
                .addGap(355, 355, 355))
        );
        victoryPaneLayout.setVerticalGroup(
            victoryPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(victoryPaneLayout.createSequentialGroup()
                .addGap(270, 270, 270)
                .addComponent(replayLevelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65)
                .addComponent(backToMainMenuButton, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(200, Short.MAX_VALUE))
        );

        containPanel.add(victoryPane, "Victory");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(containPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(containPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>

    /**
     * BUTTON NAME: CREATE GAME
     * LOCATION: MAIN MENU
     * FUNCTION: Sends user to the create game screen
     *
     * @param evt
     */
    private void creGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        cl = (CardLayout)(containPanel.getLayout());
        cl.next(containPanel);// TODO add your handling code here:
    }                                             

    /**
     * BUTTON NAME: LEVEL EDITOR
     * LOCATION: MAIN MENU
     * FUNCTION: Launches the level editor
     *
     * @param evt
     */
    private void levEditorButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        new LevelEditor.App();     
}                                               

    /**
     * BUTTON NAME: BACK
     * LOCATION: CREATE GAME MENU
     * FUNCTION: Allows the user to go back to the MAIN MENU
     *
     * @param evt
     */
    private void createBackButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        cl = (CardLayout)(containPanel.getLayout());
        cl.show(containPanel, "Main");// TODO add your handling code here:// TODO add your handling code here:
    }                                                


    /**
     * BUTTON NAME: READY
     * LOCATION: JOIN SUCCESS MENU
     * FUNCTION: Allows the user to indicate to the server that they are ready to play the game
     *
     * @param evt
     */
    private void readyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        username = userNameField.getText();       
        type = (String)playerTypeCb.getSelectedItem();
        client.setType(Special.valueOf(type));
        client.initializeUserThread();
        inputMap = new InputMap(containPanel, client);
        cl = (CardLayout)(containPanel.getLayout());
        cl.show(containPanel, "Game");
        if (username == null){
            JOptionPane.showMessageDialog(this, "Please enter username");
        }
        if (type == null){
            JOptionPane.showMessageDialog(this, "Please choose your player type");
        }
    }                                           

    /**
     * BUTTON NAME: QUIT
     * LOCATION: MAIN MENU
     * FUNCTION: Allows the user to exit the game.
     *
     * @param evt
     */
    private void quitGameButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        System.exit(0);
}                                              

    /**
     * BUTTON NAME: JOIN
     * LOCATION: MAIN MENU
     * FUNCTION: Sends user to JOIN MENU
     *
     * @param evt
     */
    private void joiButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
      cl = (CardLayout)(containPanel.getLayout());
      cl.show(containPanel, "Join");// TODO add your handling code here:
    }                                         

    /**
     * BUTTON NAME: CONNECT
     * LOCATION: JOIN MENU
     * FUNCTION: This Button starts a client to connect to the the server.  If successful, goes to SUCCESS CONNECT MENU
     *
     * @param evt
     */
    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        int client_portnum = -1;
        // Reset error images displayed on screen
        portErrorImage.setIcon(null);
        serverErrorImage.setIcon(null);
        failedToConnectImage.setIcon(null);
        try
        {
            String addrname = joinServerField.getText();
            if (addrname.equals(""))
            {
                serverErrorImage.setIcon(badServer);
            }
            client_portnum = Integer.parseInt(joinPortField.getText());
            if (client_portnum >= 1024 && client_portnum <= 65535 && !addrname.equals(""))
            {
                // tries to initiate client and connect to server
                client = new GameEngine.Client(this, addrname, client_portnum, this);
                // Checks if socket was able to connect
                if (client.getSocket() != null)
                {
                    cl = (CardLayout)(containPanel.getLayout());
                    cl.show(containPanel, "clientConnectPane");
                    AnimationPane.getGraphics().drawImage(orig, WIDTH, WIDTH, rootPane);
                }
                // Display error message otherwise
                else
                {
                    failedToConnectImage.setIcon(failedConnect);
                }
            }
            else
            {
                portErrorImage.setIcon(badPortNum);
            }
        }
        catch (NumberFormatException e1)
        {
            portErrorImage.setIcon(stringPort);
        }
}                                             

    /**
     * BUTTON NAME: CREATE
     * LOCATION: CREATE GAME MENU
     * FUNCTION: Allows user to create and host a game server.  If successful, sends user to JOIN MENU to join their own game (or join another game)
     *
     * @param evt
     */
    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        portErrorImageCreate.setIcon(null);
        serverErrorImageCreate.setIcon(null);
        // level name
        String levelString = (String) levelSelectionComboBox.getSelectedItem();
        // level enum
        Levels levelEnum = GameConstants.Levels.valueOf(levelString);
        // level byte
        byte levelByte = (byte) levelEnum.ordinal();
        // level filepath
        String cusLevelPath = cusLevelField.getText();
        try
        {
           portnum = Integer.parseInt(portField.getText());
           
           server = serAddrField.getText();
           if(portnum < 1024 || portnum > 65535)
           {
                portErrorImageCreate.setIcon(badPortNum_Create);
           }
           else
           {
                Level newLevel = null;
                try
                {
                    newLevel = XmlIO.read(cusLevelPath);
                }
                catch (DocumentException ex)
                {

                }
                catch (FileNotFoundException ex)
                {

                }
                if (newLevel != null)
                {
                    servergui = new ServerGUI(newLevel, portnum, levelByte);
                    if (servergui.isServerRunning())
                    {
                        servergui.toBack();
                        // send user to join screen, filling in port and server automatically
                        cl = (CardLayout)(containPanel.getLayout());
                        cl.show(containPanel, "Join");
                        port = Integer.toString(portnum);
                        joinPortField.setText(port);
                        joinServerField.setText("localhost");
                        if(cusLevelPath == null)
                        {
                            JOptionPane.showMessageDialog(this, "Please choose your custom level file");
                        }
                        if (port == null)
                        {
                            JOptionPane.showMessageDialog(this, "Please enter local port number");
                        }
                        if (server == null)
                        {
                            JOptionPane.showMessageDialog(this, "Please enter server address");
                        }
                    }
                    else
                    {
                        
                    }
                }
                else
                {
                    XMLErrorLabel.setIcon(invalidXMLIcon);
                }
           }

        }
        catch(NumberFormatException e1)
        {
            portErrorImageCreate.setIcon(stringPort_Create);
        }
        // TODO add your handling code here:
}                                            

    /**
     * BUTTON NAME: GO BACK
     * LOCATION: JOIN MENU
     * FUNCTION: Allows the user to return to the MAIN MENU
     *
     * @param evt
     */
    private void joinBackButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
         // Clears data from Join Screen
        portErrorImage.setIcon(null);
        serverErrorImage.setIcon(null);
        failedToConnectImage.setIcon(null);
        joinServerField.setText("");
        joinPortField.setText("");
        // Returns to Main Screen
        cl = (CardLayout)(containPanel.getLayout());
        cl.show(containPanel, "Main");
}                                              


    // TODO Later
    private void userNameFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
    }                                             
    // TODO Later
    private void replayLevelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  


}                                                 

    private void seleCustomLevelActionPerformed(java.awt.event.ActionEvent evt) {
      int returnVal = levelChooser.showOpenDialog(this);
      if (returnVal == levelChooser.APPROVE_OPTION)
      {
            customLevel = levelChooser.getSelectedFile();//get the selected file
            cusLevelField.setText(customLevel.getAbsolutePath());
      } 
    }


    /**
     * Main method to launch game.
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
               MainGui main = new MainGui();
               main.setDefaultCloseOperation(EXIT_ON_CLOSE);
               main.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel AnimationPane;
    private javax.swing.JLabel XMLErrorLabel;
    private javax.swing.JButton backToMainMenuButton;
    private javax.swing.JButton connectButton;
    private javax.swing.JPanel containPanel;
    private javax.swing.JButton creGameButton;
    private javax.swing.JButton createBackButton;
    private javax.swing.JButton createButton;
    private javax.swing.JPanel createGamePane;
    private javax.swing.JTextField cusLevelField;
    private javax.swing.JLabel customGame;
    private javax.swing.JLabel failedToConnectImage;
    private javax.swing.JPanel game;
    private javax.swing.JButton joiButton;
    private javax.swing.JButton joinBackButton;
    private javax.swing.JPanel joinGamePane;
    private javax.swing.JTextField joinPortField;
    private javax.swing.JTextField joinServerField;
    private javax.swing.JButton levEditorButton;
    private javax.swing.JFileChooser levelChooser;
    private javax.swing.JComboBox levelSelectionComboBox;
    private javax.swing.JPanel mainPane;
    private javax.swing.JOptionPane msgBox;
    private javax.swing.JComboBox playerTypeCb;
    private javax.swing.JLabel portErrorImage;
    private javax.swing.JLabel portErrorImageCreate;
    private javax.swing.JTextField portField;
    private javax.swing.JButton quitGameButton;
    private javax.swing.JButton readyButton;
    private javax.swing.JButton replayLevelButton;
    private javax.swing.JButton seleCustomLevel;
    private javax.swing.JTextField serAddrField;
    private javax.swing.JLabel serverErrorImage;
    private javax.swing.JLabel serverErrorImageCreate;
    private javax.swing.JPanel successPane;
    private javax.swing.JTextField userNameField;
    private javax.swing.JPanel victoryPane;
    // End of variables declaration

}
